﻿using cfg.enemy;
using UnityEngine;

public class EnemyStateController:ControllerBase
{
    
        public bool isFindTarget;
        public RaycastHit2D hit;
        public float  lastAttackTime;
        private EnemyInfo enemyInfo;
        private EnemyStateInfo enemyStateInfo;
        #region 被反击
        [SerializeField]protected GameObject counterSprite;
        public bool canBeCounter = false;

        public void OpenCounterWindow()
        {
            canBeCounter = true;
            counterSprite.SetActive(true);
        }
        public void CloseCounterWindow()
        {
            canBeCounter = false;
            counterSprite.SetActive(false);
        }
        
        public void TriggerStunned()
        {
            stateMachine.ChangeState(Setting.stun);
            CloseCounterWindow();
        }
        #endregion

        #region 初始化配置和状态
        protected override void InitStats()
        {   
            base.InitStats();
            characterBase.InitStats(StatType.MaxHealth,enemyInfo.MaxHealth);
            characterBase.InitStats(StatType.CurrentHealth,enemyInfo.MaxHealth);
            characterBase.InitStats(StatType.Damage,enemyInfo.AttackDamage);
            characterBase.InitStats(StatType.CurDefense,enemyInfo.Defensse);
            characterBase.InitStats(StatType.MaxDefense,enemyInfo.Defensse);
            characterBase.InitStats(StatType.CritRate,enemyInfo.CritRate);
            characterBase.InitStats(StatType.CritDamage,enemyInfo.CritDamage);
            characterBase.InitStats(StatType.DodgeRate,enemyInfo.DodgeRate);
            
            //魔法
            characterBase.InitStats(StatType.MagicResistance,enemyInfo.MagicResistance);
            if (enemyInfo.FireDamage.Count>1)
            {
                characterBase.InitStats(StatType.FireDamage,enemyInfo.FireDamage[0]);
                characterBase.InitStats(StatType.FireCumulativeValue,enemyInfo.FireDamage[1]);
            }

            if (enemyInfo.IceDamage.Count>1)
            {
                characterBase.InitStats(StatType.IceDamage,enemyInfo.IceDamage[0]);
                characterBase.InitStats(StatType.IceCumulativeValue,enemyInfo.IceDamage[1]);
            }
            if (enemyInfo.ElectricalDamage.Count>1)
            {
                characterBase.InitStats(StatType.ElectricDamage,enemyInfo.ElectricalDamage[0]);
                characterBase.InitStats(StatType.ElectricCumulativeValue,enemyInfo.ElectricalDamage[1]);
            }
            
        }
        
        protected override void InitStates()
        {
            base.InitStates();
            stateMachine.AddState(new SkeletonIdleState(this, stateMachine, Setting.idle
                , Setting.AnimatorIdle));
            stateMachine.AddState(new SkeletonMoveState(this, stateMachine, Setting.move
                , Setting.AnimatorMove));
            stateMachine.AddState(new SkeletonBattleState(this, stateMachine, Setting.battle
                , Setting.AnimatorMove));
            stateMachine.AddState(new SkeletonAttackState(this, stateMachine, Setting.attack
                , Setting.AnimatorAttack));
            stateMachine.AddState(new SkeletonStunState(this, stateMachine, Setting.stun
                , Setting.AnimatorStun));
            stateMachine.AddState(new SkeletonDeadState(this, stateMachine, Setting.dead
            ));
        }
    
        protected  virtual void SetEnemyInfo(int id)
        {
            enemyInfo = LoadCofigManager.Instance.GetEnemyInfo(id);
            enemyStateInfo = LoadCofigManager.Instance.GetEnemyStateInfo(id);
        }

        public EnemyInfo GetEnemyInfo()
        {
            return enemyInfo;   
        }

        public EnemyStateInfo GetEnemyStateInfo()
        {
            return enemyStateInfo;
        }

        #endregion

        private void FindPlayer()
        {
            hit = Physics2D.Linecast(transform.position,
                transform.position + Vector3.right * faceDir * GetEnemyStateInfo().CheckRadius,
                LayerMask.GetMask("Player"));
            isFindTarget = hit.collider != null;
        }
        
        protected override void Update()
        {
            base.Update();
            FindPlayer();
        }

        protected override void OnDrawGizmos()
        {
            base.OnDrawGizmos();
            Gizmos.color = Color.yellow;
            if (GetEnemyInfo()!=null)
            {
                Gizmos.DrawLine(transform.position, transform.position + Vector3.right * faceDir * GetEnemyStateInfo().CheckRadius);
            }
            if (GetEnemyInfo()!=null&&attackCheck!=null)
            {
                Gizmos.DrawWireSphere(attackCheck.position, GetEnemyStateInfo().AttackRadius);
            }
        }
}